#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
________________________________________________
 @Time    : 2018/6/6 上午11:04
 @Author  : NickTet
 @Site    : 
 @File    : ProxyCheck.py
 @Software: PyCharm
 _______________________________________________
 """

import sys
from threading import Thread

sys.path.append('../')
from Util.UtilFunction import  validUsefulProxy
from Manager.ProxyManager import  ProxyManager
from Util.LogHandler import LogHandler

# 校验失败次数， 超过次数删除代理
FAIL_COUNT = 1

class ProxyCheck(ProxyManager,Thread):
    def __init__(self, queue, item_dict):
        ProxyManager.__init__(self)
        Thread.__init__(self)
        self.log = LogHandler('proxy_check',file=False)
        self.queue = queue
        self.item_dict = item_dict

    def run(self):
        self.db.changeTable(self.useful_proxy_queue)
        while self.queue.qsize:
            proxy = self.queue.get()
            count = self.item_dict[proxy]
            if validUsefulProxy(proxy):
                # 验证通过计数器减1
                if count and int(count) > 0:
                    self.db.put(proxy, num=int(count)+1)
                else:
                    pass
                self.log.info('ProxyCheck:{} validation pass'.format(proxy))
            else:
                self.log.info('ProxyCheck: {} validation fail'.format(proxy))
                if count and int(count)+1 >= FAIL_COUNT:
                    self.log.info('ProxyCheck: {} fail too many, delete!'.format(proxy))
                    self.db.delete(proxy)
                else:
                    self.db.put(proxy, num=int(count) + 1)
                self.queue.task_done()


if __name__ == '__main__':
    # p = ProxyCheck()
    # p.run()
    pass